package com.beiding.delaytask.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 队列定义
 *
 * @author 丁常磊
 * @date 2021/8/12 16:06
 */

@Component
public class DelayTaskQueueConfig {

    @Value("${delay-task.namespace:delay-task}")
    private String namespace;

    /**
     * 触发队列
     * @return
     */
    @Bean
    public Queue trigger() {
        return new Queue(namespace + ".queue.trigger", true);
    }

    /**
     * 死信交换机
     * @return
     */
    @Bean
    public DirectExchange triggerExchange() {
        return new DirectExchange(namespace+".exchange.trigger");
    }

    @Bean
    public Binding bind() {
        return BindingBuilder.bind(trigger())
                .to(triggerExchange())
                .with("trigger");
    }


    //------延时队列-------

    private Queue createQueue(String name, int time) {
        return QueueBuilder.durable(name)
                .withArgument("x-message-ttl", time * 1000)    //队列中消息的有效时长
                .withArgument("x-dead-letter-exchange", namespace+".exchange.trigger")    //死信转发到的交换机
                .withArgument("x-dead-letter-routing-key", "trigger")    //死信转发时使用的路由键
                .build();
    }


    @Bean
    public Queue delayQueue0() {
        return createQueue(namespace+".binary-queue-0", (int) Math.pow(2, 0));
    }


    @Bean
    public Queue delayQueue1() {
        return createQueue(namespace+".binary-queue-1", (int) Math.pow(2, 1));
    }


    @Bean
    public Queue delayQueue2() {
        return createQueue(namespace+".binary-queue-2", (int) Math.pow(2, 2));
    }


    @Bean
    public Queue delayQueue3() {
        return createQueue(namespace+".binary-queue-3", (int) Math.pow(2, 3));
    }


    @Bean
    public Queue delayQueue4() {
        return createQueue(namespace+".binary-queue-4", (int) Math.pow(2, 4));
    }


    @Bean
    public Queue delayQueue5() {
        return createQueue(namespace+".binary-queue-5", (int) Math.pow(2, 5));
    }


    @Bean
    public Queue delayQueue6() {
        return createQueue(namespace+".binary-queue-6", (int) Math.pow(2, 6));
    }


    @Bean
    public Queue delayQueue7() {
        return createQueue(namespace+".binary-queue-7", (int) Math.pow(2, 7));
    }


    @Bean
    public Queue delayQueue8() {
        return createQueue(namespace+".binary-queue-8", (int) Math.pow(2, 8));
    }


    @Bean
    public Queue delayQueue9() {
        return createQueue(namespace+".binary-queue-9", (int) Math.pow(2, 9));
    }


    @Bean
    public Queue delayQueue10() {
        return createQueue(namespace+".binary-queue-10", (int) Math.pow(2, 10));
    }


    @Bean
    public Queue delayQueue11() {
        return createQueue(namespace+".binary-queue-11", (int) Math.pow(2, 11));
    }


    @Bean
    public Queue delayQueue12() {
        return createQueue(namespace+".binary-queue-12", (int) Math.pow(2, 12));
    }


    @Bean
    public Queue delayQueue13() {
        return createQueue(namespace+".binary-queue-13", (int) Math.pow(2, 13));
    }


    @Bean
    public Queue delayQueue14() {
        return createQueue(namespace+".binary-queue-14", (int) Math.pow(2, 14));
    }


    @Bean
    public Queue delayQueue15() {
        return createQueue(namespace+".binary-queue-15", (int) Math.pow(2, 15));
    }


    @Bean
    public Queue delayQueue16() {
        return createQueue(namespace+".binary-queue-16", (int) Math.pow(2, 16));
    }


    @Bean
    public Queue delayQueue17() {
        return createQueue(namespace+".binary-queue-17", (int) Math.pow(2, 17));
    }


    @Bean
    public Queue delayQueue18() {
        return createQueue(namespace+".binary-queue-18", (int) Math.pow(2, 18));
    }


    @Bean
    public Queue delayQueue19() {
        return createQueue(namespace+".binary-queue-19", (int) Math.pow(2, 19));
    }


}
